package com.example.ywr.controller;

import com.example.ywr.common.PageParam;
import com.example.ywr.common.PageResult;
import com.example.ywr.common.Result;
import com.example.ywr.dto.OrderRequest;
import com.example.ywr.entity.Order;
import com.example.ywr.service.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 订单控制器
 */
@Api(tags = "订单管理")
@RestController
@RequestMapping("/api/orders")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @ApiOperation("创建订单")
    @PostMapping
    public Result<Order> create(@Validated @RequestBody OrderRequest request) {
        Order order = new Order();
        BeanUtils.copyProperties(request, order);
        Order createdOrder = orderService.create(order);
        return Result.success(createdOrder);
    }

    @ApiOperation("更新订单")
    @PutMapping("/{id}")
    public Result<Order> update(@PathVariable Long id, @Validated @RequestBody OrderRequest request) {
        Order order = new Order();
        BeanUtils.copyProperties(request, order);
        order.setId(id);
        Order updatedOrder = orderService.update(order);
        if (updatedOrder == null) {
            return Result.failed("订单不存在");
        }
        return Result.success(updatedOrder);
    }

    @ApiOperation("删除订单")
    @DeleteMapping("/{id}")
    public Result<Boolean> delete(@PathVariable Long id) {
        boolean success = orderService.delete(id);
        if (success) {
            return Result.success(true);
        }
        return Result.failed("删除失败");
    }

    @ApiOperation("批量删除订单")
    @DeleteMapping("/batch")
    public Result<Boolean> batchDelete(@RequestBody List<Long> ids) {
        boolean success = orderService.batchDelete(ids);
        if (success) {
            return Result.success(true);
        }
        return Result.failed("批量删除失败");
    }

    @ApiOperation("获取订单详情")
    @GetMapping("/{id}")
    public Result<Order> getById(@PathVariable Long id) {
        Order order = orderService.getById(id);
        if (order == null) {
            return Result.failed("订单不存在");
        }
        return Result.success(order);
    }

    @ApiOperation("根据订单编号获取订单详情")
    @GetMapping("/no/{orderNo}")
    public Result<Order> getByOrderNo(@PathVariable String orderNo) {
        Order order = orderService.getByOrderNo(orderNo);
        if (order == null) {
            return Result.failed("订单不存在");
        }
        return Result.success(order);
    }

    @ApiOperation("分页查询订单列表")
    @GetMapping
    public Result<PageResult<Order>> list(PageParam pageParam) {
        PageResult<Order> pageResult = orderService.list(pageParam);
        return Result.success(pageResult);
    }

    @ApiOperation("根据用户ID查询订单")
    @GetMapping("/user/{userId}")
    public Result<PageResult<Order>> listByUserId(@PathVariable Long userId, PageParam pageParam) {
        PageResult<Order> pageResult = orderService.listByUserId(userId, pageParam);
        return Result.success(pageResult);
    }

    @ApiOperation("根据订单状态查询订单")
    @GetMapping("/status/{status}")
    public Result<PageResult<Order>> listByStatus(@PathVariable Integer status, PageParam pageParam) {
        PageResult<Order> pageResult = orderService.listByStatus(status, pageParam);
        return Result.success(pageResult);
    }

    @ApiOperation("根据用户ID和订单状态查询订单")
    @GetMapping("/user/{userId}/status/{status}")
    public Result<PageResult<Order>> listByUserIdAndStatus(@PathVariable Long userId, @PathVariable Integer status, PageParam pageParam) {
        PageResult<Order> pageResult = orderService.listByUserIdAndStatus(userId, status, pageParam);
        return Result.success(pageResult);
    }

    @ApiOperation("根据创建时间范围查询订单")
    @GetMapping("/time")
    public Result<PageResult<Order>> listByCreateTime(
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime startTime,
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime,
            PageParam pageParam) {
        PageResult<Order> pageResult = orderService.listByCreateTime(startTime, endTime, pageParam);
        return Result.success(pageResult);
    }

    @ApiOperation("更新订单状态")
    @PutMapping("/{id}/status/{status}")
    public Result<Boolean> updateStatus(@PathVariable Long id, @PathVariable Integer status) {
        boolean success = orderService.updateStatus(id, status);
        if (success) {
            return Result.success(true);
        }
        return Result.failed("更新状态失败");
    }

    @ApiOperation("批量更新订单状态")
    @PutMapping("/batch/status/{status}")
    public Result<Boolean> batchUpdateStatus(@RequestBody List<Long> ids, @PathVariable Integer status) {
        boolean success = orderService.batchUpdateStatus(ids, status);
        if (success) {
            return Result.success(true);
        }
        return Result.failed("批量更新状态失败");
    }
}